﻿using DongFangGuoKai.WMSPlatform.Domain.Model;
using DongFangGuoKai.WMSPlatform.Service.Permissions.DTO;

namespace DongFangGuoKai.WMSPlatform.Service.Permissions
{
    public partial class PermissionService : IPermissionService
    {

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool DelRole(int rid)
        {
            try
            {
                menuactionrepost.AsTenant().BeginTran();
                var robj = rolerepost.GetFirst(r => r.Id == rid);
                if (robj != null)
                {
                    if (!rolerepost.Delete(robj))
                    {
                        menuactionrepost.AsTenant().RollbackTran();
                        return false;
                    }
                    var urobj = userrolerepost.GetList(u => u.RoleId == rid);
                    if (!userrolerepost.Delete(urobj))
                    {
                        menuactionrepost.AsTenant().RollbackTran();
                        return false;
                    }
                    var rma = roleMenuActionrepost.GetList(r => r.RoleId == rid);
                    if (!roleMenuActionrepost.Delete(rma))
                    {
                        menuactionrepost.AsTenant().RollbackTran();
                        return false;
                    }
                    var rw = rolewarehouserepost.GetList(r => r.RoleId == rid);
                    if (!rolewarehouserepost.Delete(rw))
                    {
                        menuactionrepost.AsTenant().RollbackTran();
                        return false;
                    }
                    menuactionrepost.AsTenant().CommitTran();
                    return true;
                }

                return false;
            }
            catch (Exception)
            {
                menuactionrepost.AsTenant().RollbackTran();
                return false;
                throw;
            }

        }

        /// <summary>
        /// 分配权限中的列表显示
        /// </summary>
        /// <returns></returns>
        public List<MenuDto> GetRoleMenuActionList()
        {
            var mlist = menurepost.GetList();
            return GetRoleMenuActionLists(mlist, 0);
        }

        public List<MenuDto> GetRoleMenuActionLists(List<TMenus> list, int pid)
        {
            var alist = menuactionrepost.GetList();
            var malist = actionrepost.GetList();
            return list.Where(x => x.ParentId == pid).Select(x => new MenuDto
            {
                id = x.Id,
                ParentId = x.ParentId,
                MenuUrl = x.MenuUrl,
                Meta = new MetaDto
                {
                    MenuName = x.MenuName,
                    MenuIcon = x.MenuIcon,
                },
                Status = x.Status,
                DisplayOrder = x.DisplayOrder,
                children = GetRoleMenuActionLists(list, x.Id),
                Action = MenuActionById(alist, malist, x.Id)
            }).OrderBy(x => x.DisplayOrder).ToList();
        }

        public static List<ActionDto> MenuActionById(List<TMenuActions> list, List<TActions> alist, int mid)
        {
            var lists = from a in list
                        join b in alist
                        on a.ActionId equals b.Id
                        where a.MenuId == mid
                        select b;
            return lists.Select(x => new ActionDto
            {
                Id = x.Id,
                ActionName = x.ActionName
            }).ToList();
        }
    }
}
